/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web.mobile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.mobile.Mobile;
import com.thinkgem.jeesite.modules.sys.service.mobile.MobileService;

/**
 * 号码归属Controller
 * @author jin
 * @version 2018-04-16
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/mobile/mobile")
public class MobileController extends BaseController {

	@Autowired
	private MobileService mobileService;
	
	@ModelAttribute
	public Mobile get(@RequestParam(required=false) String id) {
		Mobile entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = mobileService.get(id);
		}
		if (entity == null){
			entity = new Mobile();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:mobile:mobile:view")
	@RequestMapping(value = {"list", ""})
	public String list(Mobile mobile, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Mobile> page = mobileService.findPage(new Page<Mobile>(request, response), mobile); 
		model.addAttribute("page", page);
		return "modules/sys/mobile/mobileList";
	}

	@RequiresPermissions("sys:mobile:mobile:view")
	@RequestMapping(value = "form")
	public String form(Mobile mobile, Model model) {
		model.addAttribute("mobile", mobile);
		return "modules/sys/mobile/mobileForm";
	}

	@RequiresPermissions("sys:mobile:mobile:edit")
	@RequestMapping(value = "save")
	public String save(Mobile mobile, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, mobile)){
			return form(mobile, model);
		}
		mobileService.save(mobile);
		addMessage(redirectAttributes, "保存mobile成功");
		return "redirect:"+Global.getAdminPath()+"/sys/mobile/mobile/?repage";
	}
	
	@RequiresPermissions("sys:mobile:mobile:edit")
	@RequestMapping(value = "delete")
	public String delete(Mobile mobile, RedirectAttributes redirectAttributes) {
		mobileService.delete(mobile);
		addMessage(redirectAttributes, "删除mobile成功");
		return "redirect:"+Global.getAdminPath()+"/sys/mobile/mobile/?repage";
	}

}